const { generateApi } = require('swagger-typescript-api');
const path = require('path');
const fs = require('fs');

// generateApi({
//   name: 'Myservice.ts',
//   output: path.resolve(process.cwd(), './src/services/my'),
//   url: 'http://192.168.6.30:5000/swagger/app/swagger.json',
//   templates: path.resolve(__dirname, './api-templates'),
//   modular: true,
//   // httpClientType: 'axios', // or "fetch"
//   defaultResponseAsSuccess: true,
//   generateRouteTypes: false,
//   generateResponses: true,
//   generateClient: true,
//   disableStrictSSL: true,
//   toJS: false,
//   extractRequestParams: true,
//   extractRequestBody: false,
//   prettier: {
//     printWidth: 100,
//     tabWidth: 2,
//     trailingComma: 'all',
//     parser: 'typescript'
//   },
//   defaultResponseType: 'void',
//   singleHttpClient: true,
//   cleanOutput: true,
//   enumNamesAsValues: false,
//   moduleNameFirstTag: true,
//   generateUnionEnums: false,

//   extraTemplates: [],
//   hooks: {
//     onCreateComponent: (component) => {},
//     onCreateRequestParams: (rawType) => {},
//     onCreateRoute: (routeData) => {
//       routeData.response.type = routeData.response.type
//         .replace('RestfulResultList', '')
//         .replace('RestfulResult', '')
//     },
//     onCreateRouteName: (routeNameInfo, rawRouteInfo) => {},
//     onFormatRouteName: (routeInfo, templateRouteName) => {
//       // routeInfo.responsesTypes.forEach((item) => {
//       //   item.type = item.type.replace('RestfulResultList', '').replace('RestfulResult', '')
//       // })
//       // const { moduleName } = routeInfo
//       // return (templateRouteName = templateRouteName.replace(moduleName, ''))
//     },
//     onFormatTypeName: (typeName, rawTypeName) => {},
//     onInit: (configuration) => {},
//     onParseSchema: (originalSchema, parsedSchema) => {},
//     onPrepareConfig: (currentConfiguration) => {
//       const { config } = currentConfiguration
//       const { constants } = config

//       return {
//         ...currentConfiguration,
//         config: {
//           ...config,
//           constants: {
//             ...constants,
//             SERVER_NAME: 'my',
//             SERVER_URL: 'http://192.168.6.30:5000'
//           }
//         }
//       }
//     }
//   }
// }).then(() => {
//   generateApi({
//     name: 'homeService.ts',
//     output: path.resolve(process.cwd(), './src/services/home'),
//     url: 'http://192.168.6.30:5020/swagger/app/swagger.json',
//     templates: path.resolve(__dirname, './api-templates'),
//     modular: true,
//     // httpClientType: 'axios', // or "fetch"
//     defaultResponseAsSuccess: true,
//     generateRouteTypes: false,
//     generateResponses: true,
//     generateClient: true,
//     disableStrictSSL: true,
//     toJS: false,
//     extractRequestParams: true,
//     extractRequestBody: false,
//     prettier: {
//       printWidth: 100,
//       tabWidth: 2,
//       trailingComma: 'all',
//       parser: 'typescript'
//     },
//     defaultResponseType: 'void',
//     singleHttpClient: false,
//     cleanOutput: true,
//     enumNamesAsValues: false,
//     moduleNameFirstTag: true,
//     generateUnionEnums: false,

//     extraTemplates: [],
//     hooks: {
//       onCreateComponent: (component) => {},
//       onCreateRequestParams: (rawType) => {},
//       onCreateRoute: (routeData) => {
//         routeData.response.type = routeData.response.type
//           .replace('RestfulResultList', '')
//           .replace('RestfulResult', '')
//       },
//       onCreateRouteName: (routeNameInfo, rawRouteInfo) => {},
//       onFormatRouteName: (routeInfo, templateRouteName) => {
//         routeInfo.responsesTypes.forEach((item) => {
//           item.type = item.type.replace('RestfulResultList', '').replace('RestfulResult', '')
//         })
//         const { moduleName } = routeInfo
//         return (templateRouteName = templateRouteName.replace(moduleName, ''))
//       },
//       onFormatTypeName: (typeName, rawTypeName) => {},
//       onInit: (configuration) => {},
//       onParseSchema: (originalSchema, parsedSchema) => {
//         // console.log(parsedSchema)
//         //console.log(parsedSchema)
//       },
//       onPrepareConfig: (currentConfiguration) => {
//         const { config } = currentConfiguration
//         const { constants } = config

//         return {
//           ...currentConfiguration,
//           config: {
//             ...config,
//             constants: {
//               ...constants,
//               SERVER_NAME: 'home',
//               SERVER_URL: 'http://192.168.6.30:5020'
//             }
//           }
//         }
//       }
//     }
//   })
// })

// generateApi({
//   name: 'Myservice.ts',
//   output: path.resolve(process.cwd(), './src/services/im'),
//   url: 'http://192.168.6.30:8066/swagger/Default/swagger.json',
//   templates: path.resolve(__dirname, './api-templates'),
//   modular: true,
//   // httpClientType: 'axios', // or "fetch"
//   defaultResponseAsSuccess: true,
//   generateRouteTypes: false,
//   generateResponses: true,
//   generateClient: true,
//   disableStrictSSL: true,
//   toJS: false,
//   extractRequestParams: true,
//   extractRequestBody: false,
//   prettier: {
//     printWidth: 100,
//     tabWidth: 2,
//     trailingComma: 'all',
//     parser: 'typescript'
//   },
//   defaultResponseType: 'void',
//   singleHttpClient: true,
//   cleanOutput: true,
//   enumNamesAsValues: false,
//   moduleNameFirstTag: true,
//   generateUnionEnums: false,

//   extraTemplates: [],
//   hooks: {
//     onCreateComponent: (component) => {},
//     onCreateRequestParams: (rawType) => {},
//     onCreateRoute: (routeData) => {
//       routeData.response.type = routeData.response.type
//         .replace('RestfulResultList', '')
//         .replace('RestfulResult', '')
//     },
//     onCreateRouteName: (routeNameInfo, rawRouteInfo) => {},
//     onFormatRouteName: (routeInfo, templateRouteName) => {
//       // routeInfo.responsesTypes.forEach((item) => {
//       //   item.type = item.type.replace('RestfulResultList', '').replace('RestfulResult', '')
//       // })
//       // const { moduleName } = routeInfo
//       // return (templateRouteName = templateRouteName.replace(moduleName, ''))
//     },
//     onFormatTypeName: (typeName, rawTypeName) => {},
//     onInit: (configuration) => {},
//     onParseSchema: (originalSchema, parsedSchema) => {},
//     onPrepareConfig: (currentConfiguration) => {
//       const { config } = currentConfiguration
//       const { constants } = config

//       return {
//         ...currentConfiguration,
//         config: {
//           ...config,
//           constants: {
//             ...constants,
//             SERVER_NAME: 'im',
//             SERVER_URL: 'http://192.168.6.30:8066'
//           }
//         }
//       }
//     }
//   }
// })

generateApi({
  name: 'Myservice.ts',
  output: path.resolve(process.cwd(), './src/api/test'),
  url: 'https://petstore.swagger.io/v2/swagger.json',
  templates: path.resolve(__dirname, './api-templates'),
  modular: true,
  // httpClientType: 'axios', // or "fetch"
  defaultResponseAsSuccess: true,
  generateRouteTypes: false,
  generateResponses: true,
  generateClient: true,
  disableStrictSSL: true,
  toJS: false,
  extractRequestParams: true,
  extractRequestBody: false,
  prettier: {
    printWidth: 100,
    tabWidth: 2,
    trailingComma: 'all',
    parser: 'typescript'
  },
  defaultResponseType: 'void',
  singleHttpClient: true,
  cleanOutput: true,
  enumNamesAsValues: false,
  moduleNameFirstTag: true,
  generateUnionEnums: false,

  extraTemplates: [],
  hooks: {
    onCreateComponent: (component) => {},
    onCreateRequestParams: (rawType) => {},
    onCreateRoute: (routeData) => {
      routeData.response.type = routeData.response.type
        .replace('RestfulResultList', '')
        .replace('RestfulResult', '');
    },
    onCreateRouteName: (routeNameInfo, rawRouteInfo) => {},
    onFormatRouteName: (routeInfo, templateRouteName) => {
      // routeInfo.responsesTypes.forEach((item) => {
      //   item.type = item.type.replace('RestfulResultList', '').replace('RestfulResult', '')
      // })
      // const { moduleName } = routeInfo
      // return (templateRouteName = templateRouteName.replace(moduleName, ''))
    },
    onFormatTypeName: (typeName, rawTypeName) => {},
    onInit: (configuration) => {},
    onParseSchema: (originalSchema, parsedSchema) => {},
    onPrepareConfig: (currentConfiguration) => {
      const { config } = currentConfiguration;
      const { constants } = config;

      return {
        ...currentConfiguration,
        config: {
          ...config,
          constants: {
            ...constants,
            SERVER_NAME: 'test',
            SERVER_URL: 'petstore.swagger.io'
          }
        }
      };
    }
  }
});
